﻿using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Mail;
using System.Net;

namespace SunRiseSoft.Library.Common
{
    public class MailService
    {
        private string _path = HttpContext.Current.Server.MapPath("~/App_Data/config.xml");
        private string _from;
        private string[] _to;
        private string _subject;
        private string _body;
        private bool _ssl;
        private string _type;
        private string _servername;
        private int _port;
        private string _password;
        private bool _hideerror;

        public string Servername
        {
            get { return _servername; }
            set { _servername = value; }
        }

        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string From
        {
            get { return _from; }
            set { _from = value; }
        }

        public string[] To
        {
            get { return _to; }
            set { _to = value; }
        }

        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public bool Ssl
        {
            get { return _ssl; }
            set { _ssl = value; }
        }

        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public bool Hideerror
        {
            get { return _hideerror; }
            set { _hideerror = value; }
        }

        public MailService(string strType)
        {
            _type = strType;
            LoadXMLDefault(_path);
            
        }

        public MailService(string strType, string strPath)
        {
            _type = strType;
            LoadXMLDefault(strPath);
        }

        private void LoadXMLDefault(string strPath)
        {
            try
            {
                DataSet dsMailService = new DataSet();
                DataRow[] cols;
                DataRow dw;
                int ssl;
                dsMailService.ReadXml(strPath);
                cols = dsMailService.Tables[0].Select("mailtype = '" + _type + "'");
                if (cols.Length < 1)
                { throw new Exception("在配置文件中，没有找到正确的邮件类型！"); }
                dw = cols[0];

                _servername = dw["ServerName"].ToString(); //"smtp.163.com";

                if (!int.TryParse(dw["ServerPort"].ToString(), out _port))
                { _port = 25; }

                _from = dw["FromAddress"].ToString();
                _password = dw["PassWord"].ToString();
                _subject = dw["Subject"].ToString();
                _to = dw["ToAddress"].ToString().Split(',');
                //_ssl = dw["Subject"].ToString();
                if (int.TryParse(dw["SSL"].ToString(), out ssl))
                { _ssl = (int.Parse(dw["SSL"].ToString()) != 0); }
                else
                { _ssl = false; }

                if (int.TryParse(dw["HideError"].ToString(), out ssl))
                { _hideerror = (int.Parse(dw["HideError"].ToString()) != 0); }
                else
                { _hideerror = false; }

                _body = dw["Body"].ToString();
                ;
            }
            catch (Exception ex)
            { if (!_hideerror) { throw ex; } }
        }

        public void sendmail()
        {
            // MailMessage is used to represent the e-mail being sent 
            try
            {
                using (MailMessage message = new MailMessage(_from, _from, _subject, _body))
                {
                    foreach (string CCUser in _to)
                    {
                        message.Bcc.Add(CCUser);
                    }
                    message.IsBodyHtml = false;
                    message.Body = _body;
                    SmtpClient mailClient = new SmtpClient(_servername, _port);
                    mailClient.UseDefaultCredentials = false;
                    mailClient.Credentials = new NetworkCredential(_from, _password);
                    mailClient.EnableSsl = _ssl;
                    mailClient.Send(message);
                }
            }
            catch (Exception ex)
            { if (!_hideerror) { throw ex; } }
        }

    }
}